<?php

namespace Beerus\Modules\Designer\Actions;

use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Doraemon\tools\AlibabaCloud\VirtualPhone;
use Doraemon\service\BusinessUser\EncryptPhone;
use hhz\constant\basic\UserExtendPhoneScene;
use Doraemon\model\VirtualPhone\VirtualPhoneRecord;
use Doraemon\model\Designer;
use Doraemon\model\DesignerAuth;

class shortRecommendWord extends RowAction
{
    public $name = '设置一句话推荐';
    const TOAST_ALERT = '确定要修改该设计师的推荐文案吗？';

    public function handle($id,Request $request)
    {
        $params = $request->all();
        $recomArr= [
          $params['recom_word_1'],
          $params['recom_word_2'],
          $params['recom_word_3']
        ];
        $haveReco = array_filter($recomArr);
        $recomWord = json_encode($recomArr);
        //更新表中的内容
        $res = Designer::updateDesigner($params['uid'],["short_recommend_text" => $recomWord]);
        if (!empty($haveReco)){
            //如果设置了一句话推荐的话,也需要更新认证信息表中的字段
            DesignerAuth::updateInfo($params['uid'],['have_short_recommend' => 1]);
        }else{
            //如果设置了一句话推荐的话,也需要更新认证信息表中的字段
            DesignerAuth::updateInfo($params['uid'],['have_short_recommend' => 9]);
        }
        if ($res) {
            return $this->response()->success('设置成功(＾－＾)V')->refresh();
        }else{
            return $this->response()->success('设置失败,请稍后重试o(TωT)o');
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }
        return $key;
    }

    public function form($request)
    {
        $uid = $request->uid;
        //根据uid获取设计师的一句话推荐
        $shortRecommend = Designer::getDesignerSomeInfo('short_recommend_text',$uid);
        //转成数组
        $wordArr = json_decode($shortRecommend,true);
        $this->text('uid', '设计师uid')->default($uid)->disable();
        $this->text('recom_word_1', '推荐文案1')->default($wordArr[0])->attribute('maxlength', 20);
        $this->text('recom_word_2', '推荐文案2')->default($wordArr[1])->attribute('maxlength', 20);
        $this->text('recom_word_3', '推荐文案3')->default($wordArr[2])->attribute('maxlength', 20);
        $this->hidden('uid', '用户uid')->default($uid);

        $this->confirm(self::TOAST_ALERT);

    }
}
